package org.nf.trading.service.app.circle.impl;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.dao.CircleMapper;
import org.nf.trading.dao.CommentMapper;
import org.nf.trading.dao.ProductMapper;
import org.nf.trading.dao.UserCircleMapper;
import org.nf.trading.entity.Circle;
import org.nf.trading.entity.Comment;
import org.nf.trading.entity.Post;
import org.nf.trading.entity.UserCircle;
import org.nf.trading.exception.app.circle.CircleDataException;
import org.nf.trading.service.app.circle.CircleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class CircleServiceImpl implements CircleService {
    private final CircleMapper circleMapper;
    private final CommentMapper commentMapper;
    private final UserCircleMapper userCircleMapper;
    private final ProductMapper productMapper;

    /**
     * 判断用户是否有创建圈子的条件，注：查看是否发布过商品
     *
     * @param uid
     * @param status
     * @return
     */
    @Override
    public boolean determineProduct(Integer uid, Integer status) {
        //参数不能为空
        if (uid == null || status == null){
            log.error("参数异常");
            throw new CircleDataException(500,"参数异常");
        }
        int count = productMapper.countUserIsRelease(uid, status);
        //判断是否发布商品
        if (count > 0){
            return true;
        }
        return false;
    }

    /**
     * 创建圈子
     * @param record
     * @return
     */
    @Override
    public Circle insert(Circle record) {
        //判断是否为空
        if (record == null){
            log.error("参数为空");
            throw new CircleDataException(500,"参数为空");
        }
        //添加完后拿到主键，通过主键获取值
        int insert = circleMapper.insert(record);
        Circle circle = getCircleById(record.getCircleId());
        return circle;
    }

    /**
     * 通过用户的id查询全部圈子
     * 注：使用了中间表，后期可使用redis
     * @param uid
     * @return
     */
    @Override
    public List<Circle> listCirclesByUid(Integer uid) {
        //定义集合保存全部圈子
        List<Circle> circles = new ArrayList<>();
        List<UserCircle> userCircles = userCircleMapper.listUserCircleByUId(uid);
        //遍历获取圈子id
        for (UserCircle userCircle : userCircles) {
            //拿到中间表的圈子id查询
            Circle circle = circleMapper.getCircleById(userCircle.getCircleId());
            //添加到集合中
            circles.add(circle);
        }
        return circles;
    }

    /**
     * 通过圈子id查询一个圈子(包括全部的帖子)
     * @param circleId
     * @return
     */
    @Override
    public Circle getCircleById(Integer circleId) {
        //判断是否为空
        if (circleId == null){
            log.error("参数circleId为NULL");
            throw new CircleDataException(500,"参数circleId为NULL");
        }
        Circle circle = circleMapper.selectByPrimaryKey(circleId);
        //拿到帖子信息
        List<Post> posts = circle.getPosts();
        //遍历所有帖子
        for (Post post : posts) {
            //通过帖子id查询所有的评论
            List<Comment> comments = commentMapper.listCommentsByPostId(post.getPostId());
            post.setComments(comments);
            //这里通过帖子里的用户id查询某个用户
        }
        return circle;
    }

    /**
     * 通过圈子类型的id查询全部圈子
     * @param circleTypeId
     * @return
     */
    @Override
    public List<Circle> listCirclesByTypeId(Integer circleTypeId) {
        if (circleTypeId == null){
            throw new CircleDataException(500,"参数circleTypeId为NULL");
        }
        List<Circle> circles = circleMapper.listCirclesByTypeId(circleTypeId);
        return circles;
    }

    /**
     * 查询全部圈子
     * @return
     */
    @Override
    public List<Circle> selectAll() {
        return circleMapper.selectAll();
    }

    /**
     * 通过名称查询圈子
     * 注：后期使用搜索引擎es
     * @param circleName
     * @return
     */
    @Override
    public List<Circle> listCirclesByName(String circleName) {
        if ("".equals(circleName) || circleName == null){
            log.info("参数circleName为NULL");
            throw new CircleDataException(500,"参数circleName为NULL");
        }
        return circleMapper.listCirclesByName(circleName);
    }
}
